package com.mrd.util.sendEmail;


import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


public class SendEmail {
    static int port = 25;
    // static String server = "smtp.exmail.qq.com"; // 邮件服务器地址
    static String server = "smtp.exmail.qq.com";    // 邮件服务器地址
    static String from = "佛山市佛航展翔物流有限公司";      // 发送者,显示的发件人名字

    // static String user = "server@fsshipping.com"; // 发送者邮箱地址
    // static String password = "KHxx123456"; // 密码

    static String user = "fhzx@fsshipping.com";
    static String password = "Fhzx@83837826";

    public static void sendEmail(String email, String subject, String body)
            throws UnsupportedEncodingException, Exception {
        try {
            Properties props = new Properties();
            props.put("mail.smtp.host", server);
            props.put("mail.smtp.port", String.valueOf(port));
            props.put("mail.smtp.auth", "true");
            Transport transport = null;
            Session session = Session.getDefaultInstance(props, null);
            transport = session.getTransport("smtp");
            transport.connect(server, user, password);
            MimeMessage msg = new MimeMessage(session);
            msg.setSentDate(new Date());
            InternetAddress fromAddress = new InternetAddress(user, from, "UTF-8");
            msg.setFrom(fromAddress);
            InternetAddress[] toAddress = new InternetAddress[1];
            toAddress[0] = new InternetAddress(email);
            msg.setRecipients(Message.RecipientType.TO, toAddress);
            msg.setSubject(subject, "UTF-8");
            msg.setText(body, "UTF-8");
            msg.saveChanges();
            transport.sendMessage(msg, msg.getAllRecipients());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
